feat: dynamic data in defineRouteMeta #3096
Open
+55
β46
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π Linked issue
#2974
β Type of change
π Description
In this PR I tried to add a very basic implementation of dynamic data in defineRouteMeta. It does not evaluate the meta in build time so if an approach like this is not desired, feel free to close this PR :)
The basic idea is that since we are already creating a virtual file that exports the route meta object for every route, it would make sense to have the computation in these files as well. I am searching through the route file code until the macro invocation is found. Along the way relative imports are updated with their absolute path. If the macro is found, I discard the lines after it and replace it with a default export of its argument.
As an example this route:
is transformed into this virtual file:
Though this is a very simple idea, it is working surprisingly well, allowing us to do very nice things with it like:
The meta or parts of it can also come from imports, so we can create utilities, like:
Of course there is no shortage of drawbacks/todos either:
Relative imports can not be used in the macroπ Checklist